<?php

include_once '../inc/db_util.php';

function viewBankSoal($perkuliahan) {
    $array_res = array();
    $jsArray = json_decode($perkuliahan, false);
	
	if($jsArray->{'Id'} != NULL) {
		$query = mysql_query("SELECT * FROM tb_det_pertanyaan_quis 
				WHERE (id_quis = '' OR id_quis IS NULL) AND id_det_matkul='" . $jsArray->{'Id'} . "'");
	} 
	
    while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
        $row_array['Id'] = $row['id_det_quis'];
        $row_array['Pertanyaan'] = $row['pertanyaan'];
        $row_array['Jawaban'] = $row['kode_jawaban'];
        $row_array['PilihanA'] = $row['pilihan1'];
        $row_array['PilihanB'] = $row['pilihan2'];
        $row_array['PilihanC'] = $row['pilihan3'];
        $row_array['PilihanD'] = $row['pilihan4'];
        $row_array['Quis']['Id'] = $row['id_quis'];
        $row_array['Quis']['Nama'] = $row['nama_quis'];
        $row_array['Quis']['Waktu'] = $row['waktu'] == null ? 0 : $row['waktu'];
        $row_array['Quis']['Jumlah'] = $row['jumlah_soal'] == null ? 0 : $row['jumlah_soal'];
        $row_array['Quis']['DetMatkul']['Id'] = $row['id_det_matkul'];

        array_push($array_res, $row_array);
    }
    return json_encode($array_res);
}

function viewSoalQuiz($quiz) {
    $array_res = array();
    $jsArray = json_decode($quiz, false);
	
	if($jsArray->{'Id'} != NULL) {
		$query = mysql_query("SELECT * FROM tb_det_pertanyaan_quis t 
			JOIN tb_quis d ON t.id_quis=d.id_quis
			WHERE d.id_quis='" . $jsArray->{'Id'} . "'");
	} 

    while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
        $row_array['Id'] = $row['id_det_quis'];
        $row_array['Pertanyaan'] = $row['pertanyaan'];
        $row_array['Jawaban'] = $row['kode_jawaban'];
        $row_array['PilihanA'] = $row['pilihan1'];
        $row_array['PilihanB'] = $row['pilihan2'];
        $row_array['PilihanC'] = $row['pilihan3'];
        $row_array['PilihanD'] = $row['pilihan4'];
        $row_array['Quis']['Id'] = $row['id_quis'];
        $row_array['Quis']['Nama'] = $row['nama_quis'];
        $row_array['Quis']['Waktu'] = $row['waktu'] == null ? 0 : $row['waktu'];
        $row_array['Quis']['Jumlah'] = $row['jumlah_soal'] == null ? 0 : $row['jumlah_soal'];
        $row_array['Quis']['DetMatkul']['Id'] = $row['id_det_matkul'];

        array_push($array_res, $row_array);
    }
    return json_encode($array_res);
}

function viewSoalQuizMhs($perkuliahan) {
    $array_res = array();
    $jsArray = json_decode($perkuliahan, false);
    $query = mysql_query("SELECT * FROM tb_det_pertanyaan_quis t 
        JOIN tb_quis d ON t.id_quis=d.id_quis
        WHERE d.id_quis='" . $jsArray->{'Id'} . "' ORDER BY RAND() ");

    while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
        $row_array['Id'] = $row['id_det_quis'];
        $row_array['Pertanyaan'] = $row['pertanyaan'];
        $row_array['Jawaban'] = $row['kode_jawaban'];
        $row_array['PilihanA'] = $row['pilihan1'];
        $row_array['PilihanB'] = $row['pilihan2'];
        $row_array['PilihanC'] = $row['pilihan3'];
        $row_array['PilihanD'] = $row['pilihan4'];
        $row_array['Quis']['Id'] = $row['id_quis'];
        $row_array['Quis']['Nama'] = $row['nama_quis'];
        $row_array['Quis']['Waktu'] = $row['waktu'];
        $row_array['Quis']['Jumlah'] = $row['jumlah_soal'];
        $row_array['Quis']['DetMatkul']['Id'] = $row['id_det_matkul'];

        array_push($array_res, $row_array);
    }
    return json_encode($array_res);
}

function createSoal($json) {
    $jsArray = json_decode($json, false);

    $sql = "INSERT INTO tb_det_pertanyaan_quis(id_det_matkul, pertanyaan, kode_jawaban, 
            pilihan1, pilihan2, pilihan3, pilihan4)
        VALUES (            
        '" . $jsArray->{'DetMatkul'}->{'Id'} . "',
        '" . $jsArray->{'Pertanyaan'} . "',
        '" . $jsArray->{'Jawaban'} . "',
        '" . $jsArray->{'PilihanA'} . "',
        '" . $jsArray->{'PilihanB'} . "',
        '" . $jsArray->{'PilihanC'} . "',
        '" . $jsArray->{'PilihanD'} . "'
        )";
    if (mysql_query($sql)) {
        $id = mysql_insert_id();        
    }
    return $id;
}

function createSoalQuiz($json) {
    $jsArray = json_decode($json, false);

    $sql = "INSERT INTO tb_det_pertanyaan_quis(id_quis, id_det_matkul, pertanyaan, kode_jawaban, 
            pilihan1, pilihan2, pilihan3, pilihan4)
        VALUES (            
        '" . $jsArray->{'Quis'}->{'Id'} . "',
        '" . $jsArray->{'Quis'}->{'DetMatkul'}->{'Id'} . "',
        '" . $jsArray->{'Pertanyaan'} . "',
        '" . $jsArray->{'Jawaban'} . "',
        '" . $jsArray->{'PilihanA'} . "',
        '" . $jsArray->{'PilihanB'} . "',
        '" . $jsArray->{'PilihanC'} . "',
        '" . $jsArray->{'PilihanD'} . "'
        )";
    if (mysql_query($sql)) {
        $id = mysql_insert_id();        
    }
    return $id;
}

function updateSoal($json) {
    $jsArray = json_decode($json, false);

    $sql = "UPDATE tb_det_pertanyaan_quis SET 
        pertanyaan='" . $jsArray->{'Pertanyaan'} . "', 
        kode_jawaban='" . $jsArray->{'Jawaban'} . "',
        pilihan1='" . $jsArray->{'PilihanA'} . "',
        pilihan2='" . $jsArray->{'PilihanB'} . "',
        pilihan3='" . $jsArray->{'PilihanC'} . "',
        pilihan4='" . $jsArray->{'PilihanD'} . "'
        WHERE id_det_quis = '" . $jsArray->{'Id'} . "'";

    return mysql_query($sql);
}

function deleteSoal($json) {
    $jsArray = json_decode($json, false);

    $sql = "DELETE FROM tb_det_pertanyaan_quis WHERE id_det_quis = '" . $jsArray->{'Id'} . "'";

    return mysql_query($sql);
}

function saveSoalQuiz($json) {
    $jsArray = json_decode($json, true);
    foreach($jsArray as $soal){
		$ids = $ids . $soal['Id'];
		if($soal != $jsArray[count($jsArray) - 1])
			$ids = $ids . "','";
	}

    $sql = "UPDATE tb_det_pertanyaan_quis SET id_quis = '" . $jsArray[0]['Quis']['Id'] . "'
        WHERE id_det_quis IN ('" . $ids . "')";

    return mysql_query($sql);
}

function removeSoalQuiz($json) {
    $jsArray = json_decode($json, false); 
    $sql = "UPDATE tb_det_pertanyaan_quis SET id_quis = NULL WHERE id_det_quis='" . $jsArray->{'Id'} . "'";

    return mysql_query($sql);
}

?>
